from django.core.cache import cache
from functools import wraps

from django.utils.decorators import available_attrs
from django.contrib.contenttypes.models import ContentType

def model_property_cache(func):
  def _wrapped_func(self, *args, **kwargs):
    type = ContentType.objects.get_for_model(self)
    key = str(type)+'.'+str(self.id)+'.'+func.__name__
    def lazy():
      print 'LAZY'
      v = func(self, *args, **kwargs)
      cache.set(key, v)
      return v
    print '_wrapped_func', key, 
    value = cache.get(key)
    if not value: value = lazy()
    return value
  return wraps(func, assigned=available_attrs(func))(_wrapped_func)

